<template>
	<view class="container">
		<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="80">
			<u-form-item label="来源名称" prop="name" borderBottom ref="item1">
				<u--input v-model="form.name"></u--input>
			</u-form-item>
		</u--form>
		<u-button @click="submit" color="#19BC9C">提交</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: '',
				},
				rules: {
					name: [{
						required: true,
						message: '请输入来源名称',
						trigger: ['blur', 'change']
					}]
				},
				LoginUser: {},
				sourceid: 0,
			};
		},
		onLoad(options)
		{
			this.sourceid = options.sourceid ?? 0
			this.getSourceInfoData()
		},
		onReady() {
			//onReady 为uni-app支持的生命周期之一
			this.$u.auth.check()
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			async getSourceInfoData(){
				let result = await this.$u.api.business.SourceInfo({
					sourceid: this.sourceid
				})
				if(result.code == 1)
				{
					this.form = result.data
				}
			},
			submit() {
				this.$refs.uForm.validate().then(async () => {
					let result = await this.$u.api.business.SourceEdit({
						name: this.form.name,
						sourceid: this.sourceid
					})
					if (result.code == 1) {
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete: () => {
								uni.navigateTo({
									url: '/pages/business/source/index'
								})
							}
						})
					} else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg,
						})
					}
				}).catch(errors => {})
			}
		},
	};
</script>

<style>
	.container {
		height: 100%;
		margin: 0 20rpx;
		box-sizing: border-box;
	}
</style>